﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseLinQ.DataContex;

using System.Configuration;
using System.Data.Linq;
using Core;

namespace RBAC
{
    public class TaiNguyenLogic
    {
        string strConnectionString = ConfigurationSettings.AppSettings["ConnectString"];
        RBACDataContext db = null;
        public List<tbl_TaiNguyen> lstMenu = null;
        public List<tbl_TaiNguyen> lstMenuTmp = null;
       
        /// <summary>
        /// Lay danh sach thao tác
        /// </summary>
        /// <returns></returns>
        public List<tbl_TaiNguyen> GetList()
        {
            List<tbl_TaiNguyen> lstRes = null;
            db = new RBACDataContext(strConnectionString);
            try
            {
                lstRes = (from m in db.tbl_TaiNguyens
                                          orderby m.pk_iTaiNguyenID descending
                                          where m.bKichHoat == true
                                          select m).ToList<tbl_TaiNguyen>();
            }
            catch (Exception ex)
            {
               
            }
            db = null;
            return lstRes;
        }

        /// <summary>
        /// Lấy thao tác theo ID
        /// </summary>
        /// <param name="_intTaiNguyenID"></param>
        /// <returns></returns>
        public tbl_TaiNguyen GetByID(int _intTaiNguyenID)
        {
           tbl_TaiNguyen objRes = null;
            db = new RBACDataContext(strConnectionString);
            try
            {
                objRes = (from m in db.tbl_TaiNguyens
                          where m.bKichHoat == true
                          && m.pk_iTaiNguyenID == _intTaiNguyenID
                          select m).FirstOrDefault<tbl_TaiNguyen>();
            }
            catch (Exception ex)
            {
                
            }
            return objRes;
        }

        /// <summary>
        /// Lấy theo mã
        /// </summary>
        /// <param name="_strMaTaiNguyen"></param>
        /// <returns></returns>
        public tbl_TaiNguyen GetByCode(string _strMaTaiNguyen)
        {
            tbl_TaiNguyen objRes = null;
            db = new RBACDataContext(strConnectionString);
            try
            {
                objRes = (from m in db.tbl_TaiNguyens
                          where m.bKichHoat == true
                          && m.sMaTaiNguyen == _strMaTaiNguyen
                          select m).FirstOrDefault<tbl_TaiNguyen>();
            }
            catch (Exception ex)
            {
                
            }
            return objRes;
        }
        public tbl_TaiNguyen GetByCodeID(string strMaTaiNguyen, int ID)
        {
            tbl_TaiNguyen objRes = null;
            db = new RBACDataContext(strConnectionString);
            try
            {
                objRes = (from m in db.tbl_TaiNguyens
                          where m.bKichHoat == true
                          && m.pk_iTaiNguyenID != ID
                          && m.sMaTaiNguyen == strMaTaiNguyen
                          select m).FirstOrDefault<tbl_TaiNguyen>();
            }
            catch (Exception ex)
            {
               
            }
            return objRes;
        }

        /// <summary>
        /// Them moi thao tác
        /// </summary>
        /// <param name="_objTaiNguyen"></param>
        /// <returns></returns>
        public bool Insert(tbl_TaiNguyen _objTaiNguyen)
        {
            bool blnFalse = false;
            db = new RBACDataContext(strConnectionString);
            try
            {
                _objTaiNguyen.dNgayTao = DateTime.Now;
                _objTaiNguyen.dNgayCapNhat = DateTime.Now;
                db.tbl_TaiNguyens.InsertOnSubmit(_objTaiNguyen);
                db.SubmitChanges();
                blnFalse = true;
            }
            catch (Exception ex)
            {
               
            }
            db = null;
            return blnFalse;
        }

        /// <summary>
        /// Cap nhat thong tin thao tác
        /// </summary>
        /// <param name="_objTaiNguyenNew"></param>
        /// <returns></returns>
        public bool Update(tbl_TaiNguyen _objTaiNguyenNew)
        {
            bool blnRes = false;
            db = new RBACDataContext(strConnectionString);
            try
            {
                tbl_TaiNguyen objTaiNguyenOld = (from m in db.tbl_TaiNguyens
                                       where m.pk_iTaiNguyenID == _objTaiNguyenNew.pk_iTaiNguyenID
                                                 select m).FirstOrDefault<tbl_TaiNguyen>();
                if (objTaiNguyenOld != null && objTaiNguyenOld.pk_iTaiNguyenID > 0)
                {
                    objTaiNguyenOld.sMaTaiNguyen = _objTaiNguyenNew.sMaTaiNguyen;
                    objTaiNguyenOld.sTenTaiNguyen = _objTaiNguyenNew.sTenTaiNguyen;
                    objTaiNguyenOld.sGhiChu = _objTaiNguyenNew.sGhiChu;
                    objTaiNguyenOld.fk_iNguoiCapNhatID = _objTaiNguyenNew.fk_iNguoiCapNhatID;
                    objTaiNguyenOld.dNgayCapNhat = DateTime.Now;
                    db.SubmitChanges();
                    blnRes = true;
                }
            }
            catch (Exception ex)
            {
              
            }
            db = null;
            return blnRes;
        }

        /// <summary>
        /// Xoa thao tác
        /// </summary>
        /// <param name="_pk_iTaiNguyenID"></param>
        /// <returns></returns>
        public bool Delete(long _pk_iTaiNguyenID)
        {
            bool blnRes = false;
            db = new RBACDataContext(strConnectionString);
            try
            {
                tbl_TaiNguyen objTaiNguyenOld = (from m in db.tbl_TaiNguyens
                                       where m.pk_iTaiNguyenID == _pk_iTaiNguyenID
                                                 select m).FirstOrDefault<tbl_TaiNguyen>();
                if (objTaiNguyenOld != null && objTaiNguyenOld.pk_iTaiNguyenID > 0)
                {
                    objTaiNguyenOld.bKichHoat = false;
                    db.SubmitChanges();
                    blnRes = true;
                }
            }
            catch (Exception ex)
            {
                
            }
            db = null;
            return blnRes;
        }
    }
}
